package cn.cars.service.impl;

import cn.cars.dao.CarMapper;
import cn.cars.pojo.Car;
import cn.cars.service.CarService;
import cn.cars.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class CarServiceImpl implements CarService {
    private SqlSession sqlSession = MyBatisUtil.createSqlSession();
    private CarMapper mapper = sqlSession.getMapper(CarMapper.class);

    @Override
    public List<Car> carListPage(String carName, String carType, int startIndex, int pageSize) {
        return mapper.carListPage(carName, carType, startIndex, pageSize);
    }

    @Override
    public int count(String carName, String carType) {
        return mapper.count(carName, carType);
    }

    @Override
    public Car findCarById(int carId) {
        return mapper.findCarById(carId);
    }

    @Override
    public int modify(Car car) {
        int result = 0;
        try {
            result = mapper.modify(car);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            result = 0;
            sqlSession.rollback();
        }
        return result;
    }
}
